package com.zyh.demo.reflect;

import com.zyh.demo.domain.Student;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author zyh
 * @Date 2024/8/14 9:18
 * @Description
 */

public class ReflectTest1 {
    /*
    获取权限修饰符
     */
    @Test
    public void test5() throws Exception {
        //获取类对象
        Class c = Class.forName("com.zyh.demo.domain.Student");
        //获取age属性
        Field ageField = c.getDeclaredField("age");
        //获取权限修饰符的魔法值
        int modifiers = ageField.getModifiers();
        System.out.println("modifiers = " + modifiers);
        System.out.println("-------------------");
        //全类名和简写名
        String name = c.getName();
        System.out.println("name = " + name);
        String simpleName = c.getSimpleName();
        System.out.println("simpleName = " + simpleName);
    }

    /*
    获取类的方法
     */
    @Test
    public void test4() throws Exception {
        //获取类对象
        Class c = Class.forName("com.zyh.demo.domain.Student");
        //获取类的方法
        Method setName = c.getMethod("setName", String.class);
        System.out.println("setName = " + setName.getName());
        System.out.println("--------------------");
        //拿到包含继承于父类的方法
        Method[] methods = c.getMethods();
        for (Method method : methods) {
            System.out.println("method = " + method.getName());
        }
        System.out.println("-------------------------");
        //拿到不包含继承于父类的方法
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

    /*
    获取类的构造方法
     */
    @Test
    public void test3() throws Exception {
        //获取类对象
        Class c = Class.forName("com.zyh.demo.domain.Student");
        Constructor cons1 = c.getConstructor();
        System.out.println("cons1 = " + cons1);
        Constructor cons2 = c.getConstructor(String.class, Integer.class);
        System.out.println("cons2 = " + cons2);
        System.out.println("------------------");
        Constructor[] declaredConstructors = c.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("declaredConstructor = " + declaredConstructor);
        }
    }

    /*
    获取对象的属性
     */
    @Test
    public void test2() throws Exception {
        //1.获取类对象
        Class c = Class.forName("com.zyh.demo.domain.Student");
        //获取类的属性
        Field field = c.getField("name");
        System.out.println("field = " + field.getName());
        System.out.println("----------------");
        //只能拿到public修饰的属性
        Field[] fields = c.getFields();
        for (Field field1 : fields) {
            System.out.println(field1.getName());
        }
        System.out.println("---------------------");
        //可以拿到任意权限修饰的属性
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }
    }

    /*
    拿到类名
     */
    @Test
    public void test1() throws ClassNotFoundException {
        //拿到全类名
        Class c1 = Class.forName("com.zyh.demo.domain.Student");
        System.out.println("c1 = " + c1.getName());

        //类名调用class()方法
        Class c2 = Student.class;
        System.out.println("c2 = " + c2.getName());

        //通过对象调用getClass()方法
        Class c3 = new Student().getClass();
        System.out.println("c3 = " + c3.getName());
        System.out.println(c1==c2);
        System.out.println(c1==c3);
    }
}
